package br.edu.ufcg.dsc.actions;

import br.edu.ufcg.dsc.domain.User;
import br.edu.ufcg.dsc.exceptions.UserNotLoggedException;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseLoggedAction extends ActionSupport {

	private static final long serialVersionUID = 5460611316058849072L;

	protected User loggedUser;

	@Override
	public String execute() throws Exception {
		loggedUser = (User) ActionContext.getContext().getSession().get("user");

		if (loggedUser == null)
			throw new UserNotLoggedException();

		return executeIfLogged();
	}

	protected abstract String executeIfLogged() throws Exception;

	@Override
	public void validate() {
		loggedUser = (User) ActionContext.getContext().getSession().get("user");

		if (loggedUser == null)
			throw new UserNotLoggedException();
	}
}
